package com.cz.ssyx.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.ssyx.client.order.OrderFeignClient;
import com.cz.ssyx.common.exception.MyException;
import com.cz.ssyx.common.result.ResultCodeEnum;
import com.cz.ssyx.enums.PaymentStatus;
import com.cz.ssyx.enums.PaymentType;
import com.cz.ssyx.model.order.OrderInfo;
import com.cz.ssyx.model.order.PaymentInfo;
import com.cz.ssyx.mq.constant.MqConst;
import com.cz.ssyx.mq.service.RabbitService;
import com.cz.ssyx.payment.mapper.PaymentInfoMapper;
import com.cz.ssyx.payment.service.PaymentInfoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/5/8 21:06
 * @version: 1.0
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private RabbitService rabbitService;

    //根据订单编号获取支付记录
    @Override
    public PaymentInfo getPaymentInfoByOrderNo(String orderNo) {
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>()
                .eq(PaymentInfo::getOrderNo, orderNo));
        return paymentInfo;
    }

    //保存支付记录
    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        //远程调用根据orderNo获取订单信息
        OrderInfo order = orderFeignClient.getOrderInfo(orderNo);
        if (order == null) {
            throw new MyException(ResultCodeEnum.DATA_ERROR);
        }
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(order.getId());
        paymentInfo.setPaymentType(PaymentType.WEIXIN);
        paymentInfo.setUserId(order.getUserId());
        paymentInfo.setOrderNo(order.getOrderNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);
        String subject = "userID为：" + order.getUserId() + "的用户下的单";
        paymentInfo.setSubject(subject);
        //paymentInfo.setTotalAmount(order.getTotalAmount());
        //TODO 统一支付0.02元为了测试
        paymentInfo.setTotalAmount(new BigDecimal("0.02"));
        this.save(paymentInfo);
        return paymentInfo;
    }

    @Override
    public void paySuccess(String orderNo, Map<String, String> resultMap) {
        //修改支付记录表状态
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>()
                .eq(PaymentInfo::getOrderNo, orderNo));
        if (paymentInfo.getPaymentStatus() == PaymentStatus.PAID) {
            return;
        }
        //没有支付，更新
        paymentInfo.setPaymentStatus(PaymentStatus.PAID);
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackContent(resultMap.toString());
        this.updateById(paymentInfo);

        //TODO 整合RabbitMQ 修改订单记录，减库存
        rabbitService.sendMessage(MqConst.EXCHANGE_PAY_DIRECT,
                MqConst.ROUTING_PAY_SUCCESS,
                orderNo);
    }
}
